package top.jybill.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.Order;
import top.jybill.domain.ResponseData;
import top.jybill.service.IOrderService;
import top.jybill.service.Impl.IOrderServiceImpl;
import top.jybill.service.Impl.ProductServiceImpl;

@Controller
@RequestMapping("/order")
public class OrderController {

  @Autowired
  protected ResponseData res;
  @Autowired
  private IOrderService orderService;

  /**
   * 分页查询订单
   * @param page
   * @return
   */
  @RequestMapping(path = "/findOrders", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findOrders(MyPageInfo page) {
    res.clean();
//    System.out.println(page);
    MyPageInfo retInfo = orderService.findOrdersByPage(page);
    res.dataPut("page", retInfo);
    res.metaPut("status", 200);
    res.metaPut("msg", "成功");
    return res;
  }

  /**
   * 分局id查询订单
   * @param id
   * @return
   */
  @RequestMapping(path = "/findOrderInfo", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findOrderInfo(String id) {
    res.clean();

//    System.out.println(id);
    Order order = orderService.findOrderDetailById(id);
    res.metaPut("status", 200);
    res.metaPut("msg", "ok");
    res.dataPut("order", order);
    return res;
  }

  /**
   * 删除订单
   * @param oid
   * @return
   */
  @RequestMapping(path = "/delOrder/{oid}", method = RequestMethod.DELETE)
  @ResponseBody
  public ResponseData delOrder(@PathVariable("oid") String oid) {
    res.clean();
    orderService.delOrder(oid);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }
}
